package com.baseframe.sys.service.impl;

import java.util.List;
import java.util.Map;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baseframe.common.annotation.DataFilter;
import com.baseframe.common.utils.PageUtils;
import com.baseframe.sys.entity.FrameVisitLog;
import com.baseframe.sys.service.IFrameVisitLogService;
import com.baseframe.sys.service.dao.FrameVisitLogRepository;

/**
 * 
 * @author Administrator
 *
 */
@Service("frameVisitLogService")
public class FrameVisitLogServiceImpl implements IFrameVisitLogService {

	@Autowired
	private FrameVisitLogRepository frameVisitLogRepository;

	@Override
	public List<FrameVisitLog> selectList() {
		return frameVisitLogRepository.findAll();
	}

	@PersistenceContext
	private EntityManager em;

	@Override
	@DataFilter(subDept = true, user = false)
	public PageUtils queryPage(Map<String, Object> params) {
		String visitusername = (String) params.get("visitusername");
		int pageSize = Integer.parseInt((String) params.get("limit"));
		int page = Integer.parseInt((String) params.get("page"));
		String sidx = (String) params.get("sidx");
		String order = (String) params.get("order");
		String chql = "select count(1) from FrameVisitLog t where 1=1";
		String hql = "from FrameVisitLog t where 1=1";
		if (visitusername != null && !visitusername.trim().equals("")) {
			chql += " and t.visitusername like '%" + visitusername + "%'";
			hql += " and t.visitusername like '%" + visitusername + "%'";
		}
		if (sidx != null && !sidx.equals("")) {
			hql += " order by t." + sidx + " " + order;
		}
		int total = Integer.parseInt(em.createQuery(chql).getSingleResult()
				.toString());

		List<FrameVisitLog> list = em.createQuery(hql)
				.setFirstResult((page - 1) * pageSize)
				.setMaxResults(page * pageSize).getResultList();

		PageUtils p = new PageUtils(list, total, pageSize, page);
		return p;
	}

	@Override
	public void insert(FrameVisitLog frameVisitLog) {
		frameVisitLogRepository.save(frameVisitLog);
	}

}
